$(function () {
  xiangmushouzhi = 1;
  initxiangmushouzhiData();
})

function initxiangmushouzhiData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");

  var obj1 = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 62,
    chartNo: 1
  };
  var obj2 = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 62,
    chartNo: 2
  };
  var obj3 = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 62,
    chartNo: 3
  };
  ApiGet("getdata", obj1, function (e) {
    // console.log(e)
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    let data = chartData[0].data[0]
    $(".collection").text(pricefmt((data.key01 / 10000).toFixed(2)));
    $(".expenditure").text(pricefmt((data.key02 / 10000).toFixed(2)));

  });
  ApiGet("getdata", obj2, function (e) {
    // console.log(e)
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    console.log(chartData, "chartData2");
    let data = chartData[0].data
    getallxiangmushouzhiChart(data); //全所项目收支累计情况
  });
  ApiGet("getdata", obj3, function (e) {
    // console.log(e)
    var json = JSON.parse(e);
    var chartData = json.items[0].charts;
    console.log(chartData, "chartData3");
    let data = chartData[0].data
    getgenbumenChart(data); //各部门收支情况对比
  });
}

function getallxiangmushouzhiChart(data) {
  let legendData = ['项目收款', '成本支出']
  let xiangmushoukuan = [];
  let chengbenzhichu = [];
  for (let i = 0; i < data.length; i++) {
    xiangmushoukuan.push((data[i].key02 / 10000).toFixed(2))
    chengbenzhichu.push((data[i].key03 / 10000).toFixed(2))
  }
  let leijirate = [{
      name: '项目收款',
      smooth: true, //平滑曲线显示
      symbol: 'circle', //标记的图形为实心圆
      symbolSize: 8, //标记的大小
      itemStyle: {
        normal: {
          color: '#165DFF',
        },
      },
      showSymbol: false,
      type: 'line',
      lineStyle: {
        width: 2
      },
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(230,238,255,0.8)'
          },
          {
            offset: 1,
            color: 'rgba(230,238,255,0.2)'
          }
        ])
      },
      data: xiangmushoukuan
    },
    {
      name: '成本支出',
      smooth: true, //平滑曲线显示
      symbol: 'circle', //标记的图形为实心圆
      symbolSize: 8, //标记的大小
      itemStyle: {
        normal: {
          color: '#20C997',
        },
      },
      showSymbol: false,
      lineStyle: {
        width: 2
      },
      type: 'line',
      areaStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(212,245,245,0.8)'
          },
          {
            offset: 1,
            color: 'rgba(212,245,245,0.2)'
          }
        ])
      },
      data: chengbenzhichu
    }
  ]
  have('all-xiangmushouzhi') ? lineChartMD('all-xiangmushouzhi', leijirate, legendData) : ''
}

function getgenbumenChart(data) {
  let xData = [];
  let xiangmushoukuan = [];
  let chengbenzhichu = [];
  data.forEach((item) => {
    xiangmushoukuan.push((item.key02 / 10000).toFixed(2));
    chengbenzhichu.push((item.key03 / 10000).toFixed(2));
    xData.push(item.key01);
  });
  let legendData = ["项目收款", "成本支出"];
  let Data = [{
      name: "项目收款",
      type: "bar",
      barWidth: "45",
      barGap: 0,
      itemStyle: {
        color: "#165DFF",
      },
      label: {
        show: true,
        position: 'top',
        distance: 10,
        color: '#1D2129',
        fontWeight: 'bold'
      },
      data: xiangmushoukuan,
    },
    {
      name: "成本支出",
      type: "bar",
      barWidth: "45",
      barGap: 0.25,
      itemStyle: {
        color: "#20C997",
      },
      label: {
        show: true,
        position: 'top',
        distance: 10,
        color: '#1D2129',
        fontWeight: 'bold'
      },
      data: chengbenzhichu,
    },
  ]
  have('genbumen') ? yueduqushi('genbumen', xData, legendData, Data) : ''
}